﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RN.ERP.Web.ModelService;
using Newtonsoft.Json;
using Fly.Common.Linq;
using Fly.Common.WorkFlow;
using Telerik.Web.UI;
using Telerik.Web.UI.Skins;
using System.Configuration;

namespace RN.ERP.Web.Enterprise
{
    public partial class EnterpriseMachineInfoList : BasePage/*System.Web.UI.Page*/
    {
        protected string AreaCode
        {
            get
            {
                return UserInfo.CurrentArea;
            }
        }
        protected string RoleId
        {
            get
            {
                return UserInfo.RoleId;
            }
        }
        private EnterpriseService.EnterpriseServiceClient enterpriseServiceClient = new EnterpriseService.EnterpriseServiceClient();
        private BasicService.BasicServiceClient basicServiceClient = new BasicService.BasicServiceClient();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hdnClassID.Value = Request["classId"];
                //InitStatisticsCondition();
                ////100系统特殊功能-锁定企业
                if (UserInfo.RoleType == "Manager" && SystemID == "100")
                {
                    ListMachines.Columns[9].Visible = true;
                }
                string appid = ConfigurationManager.AppSettings["AppId"];
            }
        }

        /// <summary>
        /// 客户端grid刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            if (e.Argument == "Rebind")
            {
                ListMachines.MasterTableView.SortExpressions.Clear();
                ListMachines.MasterTableView.GroupByExpressions.Clear();
                ListMachines.Rebind();
            }

        }
        protected void ListMachines_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            MachineDataBind();
        }

        /// <summary>
        /// 绑定设备数据列表
        /// </summary>
        private void MachineDataBind()
        {
            int pageIndex = ListMachines.CurrentPageIndex;
            int pageSize = ListMachines.PageSize;
            int count;
            bool? isOwn;
            if (ddlIsOwn.SelectedValue == "1")
            {
                isOwn = true;
            }
            else if (ddlIsOwn.SelectedValue == "0")
            {
                isOwn = false;
            }
            else
            {
                isOwn = null;
            }
            Guid corpGuid = new Guid(UserInfo.CorpUserGuid);
            if (RoleId.Equals("88") || RoleId.Equals("101"))
            {
                corpGuid = new Guid(Context.Request["corpUserGuid"]);
            }
            string areaCode = UserInfo.CurrentArea;
            var result = enterpriseServiceClient.GetCorpMachineInfoList(out count, pageIndex, pageSize, corpGuid, isOwn, areaCode);
            var MachineInfolist = JsonConvert.DeserializeObject<IList<tb_corp_MachineInfo>>(result);
            ListMachines.AllowCustomPaging = true;
            ListMachines.VirtualItemCount = count;
            ListMachines.DataSource = MachineInfolist;
        }



        protected void ddlIsOwn_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            MachineDataBind();
            ListMachines.Rebind();
        }

        //protected string GetCorpClassKind(string type)
        //{
        //    string output = string.Empty;
        //    if (!String.IsNullOrEmpty(type))
        //    {
        //        Dictionary<string, string> CorpTypeDic = JsonConvert.DeserializeObject<Dictionary<string, string>>
        //            (Application[ApplicationConstant.APPLICATION_CORPUSERTYPE].ToString());
        //        try
        //        {
        //            output = CorpTypeDic[type];
        //        }
        //        catch
        //        {
        //            output = "";
        //        }
        //    }
        //    return output;
        //}

        //private string GetCondition()
        //{
        //    List<QueryCompanyInfoCondition> queryCompanyInfoList = new List<QueryCompanyInfoCondition>();
        //    QueryCompanyInfoCondition queryCompanyInfoCondition = new QueryCompanyInfoCondition();
        //    string classId = Request.QueryString["classId"];
        //    string corpName = txtCorpName.Text.Trim();
        //    if (listFlow.Count > 0)
        //    {
        //        foreach (var flow in listFlow)
        //        {
        //            QueryCompanyInfoCondition con = new QueryCompanyInfoCondition();
        //            int clsId = -1;
        //            if (!string.IsNullOrEmpty(classId) && int.TryParse(classId, out clsId))
        //            {
        //                con.classID = new ParameterValue<int?>() { O = OptEnum.Equal, V = clsId };
        //            }
        //            if (!string.IsNullOrEmpty(corpName))
        //            {
        //                con.CorpName = new ParameterValue<string>() { O = OptEnum.Like, V = corpName };
        //            }
        //            ////解决烟台地区查看外地企业需求,这里说是地区号换字段了，但是前台获取不到变更后的字段，暂时还取以前的账号 2014-01-02 毛凯
        //            if (UserInfo.AreaCode.Length >= 4)
        //            {
        //                con.AreaCode = new ParameterValue<string>() { O = OptEnum.StartWith, V = UserInfo.AreaCode };
        //            }
        //            con.FlowCode = new ParameterValue<string>() { O=OptEnum.Equal,V=flow};
        //            queryCompanyInfoList.Add(con);
        //        }
        //    }
        //    else
        //    {
        //        int clsId = -1;
        //        if (!string.IsNullOrEmpty(classId) && int.TryParse(classId, out clsId))
        //        {
        //            queryCompanyInfoCondition.classID = new ParameterValue<int?>() { O = OptEnum.Equal, V = clsId };
        //        }
        //        if (!string.IsNullOrEmpty(corpName))
        //        {
        //            queryCompanyInfoCondition.CorpName = new ParameterValue<string>() { O = OptEnum.Like, V = corpName };
        //        }
        //        queryCompanyInfoCondition.FlowCode = new ParameterValue<string>() { O = OptEnum.Like, V = "019" };
        //        ////解决烟台地区查看外地企业需求,这里说是地区号换字段了，但是前台获取不到变更后的字段，暂时还取以前的账号 2014-01-02 毛凯
        //        if (UserInfo.AreaCode.Length >= 4)
        //        {
        //            queryCompanyInfoCondition.AreaCode = new ParameterValue<string>() { O = OptEnum.StartWith, V = UserInfo.AreaCode };
        //        }
        //        queryCompanyInfoList.Add(queryCompanyInfoCondition);
        //    }
        //    return JsonConvert.SerializeObject(queryCompanyInfoList);
        //}

        //private void InitStatisticsCondition()
        //{
        //    string statistics = Request.QueryString["statistics"];
        //    if (!string.IsNullOrEmpty(statistics) && statistics == "1")
        //    {
        //        string flowcode = Request.QueryString["flowcode"];
        //        flowcode = Server.UrlDecode(flowcode);
        //        List<QueryCompanyInfoCondition> conditionList = new List<QueryCompanyInfoCondition>();
        //        if (string.IsNullOrEmpty(flowcode))
        //        {
        //            var con = CreateCommonCompanyCondition();
        //            con.FlowCode = new ParameterValue<string>() { O=OptEnum.Like,V="019"};
        //            conditionList.Add(con);
        //        }
        //        else
        //        {
        //            string[] flows = flowcode.Split('|');
        //            foreach (string f in flows)
        //            {
        //                var con = CreateCommonCompanyCondition();
        //                con.FlowCode = new ParameterValue<string>() { O=OptEnum.Equal,V=f};
        //                conditionList.Add(con);
        //            }
        //        }
        //        hdnCondition.Value = JsonConvert.SerializeObject(conditionList);
        //    }
        //}

        //private QueryCompanyInfoCondition CreateCommonCompanyCondition()
        //{
        //    string areaCode = Request.QueryString["areacode"].Trim();
        //    string classid = Request.QueryString["classid"];
        //    QueryCompanyInfoCondition con = new QueryCompanyInfoCondition();
        //    if (!string.IsNullOrEmpty(areaCode))
        //    {
        //        if (areaCode.EndsWith("99"))
        //        {
        //            con.AreaCode = new ParameterValue<string>() { O = OptEnum.NotStartWith, V = UserInfo.CurrentArea };
        //        }
        //        else
        //        {
        //            con.AreaCode = new ParameterValue<string>() { O = OptEnum.StartWith, V = areaCode };
        //        }
        //    }
        //    else
        //    {
        //        con.AreaCode = new ParameterValue<string>() { O=OptEnum.StartWith,V=UserInfo.AreaCode};
        //    }
        //    if (!string.IsNullOrEmpty(classid))
        //    {
        //        int clsId = 0;
        //        int.TryParse(classid, out clsId);
        //        con.classID = new ParameterValue<int?>() { O = OptEnum.Equal, V = clsId };
        //    }
        //    return con;
        //}
    }
}
